package com.hrpmar.controller;


import com.hrpmar.pojo.Menu;
import com.hrpmar.pojo.Worker;
import com.hrpmar.service.PermissionService;
import com.hrpmar.utils.Constants;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by c on 2017-11-07.
 */
@Controller
public class IndexController {
    @Resource
    private PermissionService permissionService;

    @RequestMapping("/index")
    public String index(HttpSession session, Model model, HttpServletRequest request){
        if(session.getAttribute(Constants.LOGIN_USER)==null){
            request.setAttribute("aa","update");
            return "forward:login.jsp";
        }
        Worker worker = (Worker)session.getAttribute(Constants.LOGIN_USER);
        Integer roleId=worker.getRoleId();
        Map<Menu, List<Menu>> menus = permissionService.searchMenus(roleId);
        model.addAttribute("menus",menus);
        return "index";
    }

    @GetMapping("/close")
    public String close(HttpSession session, HttpServletRequest request){
        session.removeAttribute(Constants.LOGIN_USER);
        request.setAttribute("aa","close");
        return "forward:login.jsp";
    }

}
